﻿namespace FasadPattern
{
    using System;
    using FasadPattern.FileWorker;
    using FasadPattern.SerializeWorker;
    using FasadPattern.SerializeWorker.JSONObjects;

    /// <summary>
    /// Class which combines different controllers in one
    /// </summary>
    public class Fasad
    {
        #region Public Logic

        /// <summary>
        /// Writes serialized ojbect into file
        /// </summary>
        /// <param name="fileName">Destanation file</param>
        /// <param name="obj">Object for serialization</param>
        /// <exception cref="ArgumentNullException">If input parameters are nullable</exception>
        public void PackEntityToFile(string fileName, object obj)
        {
            if (obj == null)
                throw new ArgumentNullException();

            if (fileName == null)
                throw new ArgumentNullException();

            m_FileBuilder.BuildEmptyFile(fileName);
            JSONObject jsonObj = m_JSONSerializer.Serialize(obj);
            MessageBuilder messageBuilder = new MessageBuilder(jsonObj.EncodedStream());
            m_FileBuilder.WriteMessage(messageBuilder);
        }

        /// <summary>
        /// Gets file content
        /// </summary>
        /// <returns>File content</returns>
        public string GetFileContent()
        {
            return m_FileBuilder.ReadMessage();
        }

        #endregion

        #region Private Fields

        private FileBuilder m_FileBuilder = new FileBuilder();
        private JSONSerializer m_JSONSerializer = new JSONSerializer();

        #endregion
    }
}
